package com.paradogs.framework.core.config;

import com.paradogs.framework.core.exceptions.GlobalExceptionDispatch;
import com.paradogs.framework.core.netty.clients.common.ClientsProperties;
import com.paradogs.framework.core.netty.clients.common.PRServerClientChannelInitializer;
import com.paradogs.framework.core.netty.clients.common.PRServerClientMsgHandler;
import com.paradogs.framework.core.netty.clients.master.MasterProperties;
import com.paradogs.framework.core.netty.*;
import com.paradogs.framework.core.netty.clients.master.PRMasterClientChannelInitializer;
import com.paradogs.framework.core.netty.clients.master.PRMasterClientMsgHandler;
import com.paradogs.framework.core.netty.server.PRAbServerMsgHandler;
import com.paradogs.framework.core.netty.server.PRServerChannelInitializer;
import com.paradogs.framework.core.utils.MsgUtils;
import com.paradogs.framework.core.utils.PRAsync;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;

/**
 * @author: yumi
 * @date: 2023/8/26  17:22
 * @Description: TODO
 */
@AutoConfiguration // @AutoConfiguration 与 @Configuration 作用相同？，规范使用，代表自动装配
public class NettyConfiguration {

    @Bean
    public ProtocolProperties protocolProperties() {
        return new ProtocolProperties();
    }

    @Bean
    public MasterProperties masterProperties() {return new MasterProperties();}

    @Bean
    public ClientsProperties clientsProperties() {return new ClientsProperties();}

    @Bean
    public PRMasterClientChannelInitializer masterClientChannelInitializer(PRMasterClientMsgHandler msgHandler) {
        return new PRMasterClientChannelInitializer(msgHandler);
    }

    @Scope("prototype") // @Scope 代表了懒加载，启动时不会初始化
    @Bean
    public PRServerClientChannelInitializer serverClientChannelInitializer(PRServerClientMsgHandler msgHandler) {
        return new PRServerClientChannelInitializer(msgHandler);
    }

    @Bean
    public PRMasterClientMsgHandler masterClientMsgHandler() {
        return new PRMasterClientMsgHandler();
    }

    @Bean
    public GlobalExceptionDispatch globalExceptionDispatch() {
        return new GlobalExceptionDispatch();
    }

    @Bean
    public PRServerChannelInitializer serverChannelInitializer(PRAbServerMsgHandler msgHandler) {
        return new PRServerChannelInitializer(msgHandler);
    }

    @Bean
    @Scope("prototype")
    public PRCodec serverCodec() {
        return new PRCodec();
    }

    @Bean
    public MsgUtils msgUtils() {
        return new MsgUtils();
    }

    @Bean
    public PRAsync pRAsync() {return new PRAsync();}

}
